package sn.gandal.mobile.kalpet.data.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import sn.gandal.mobile.kalpet.data.entities.Account;
import sn.gandal.mobile.kalpet.server.dto.AuthDTO;
import sn.gandal.mobile.kalpet.server.dto.CompteCouponDTO;
import sn.gandal.mobile.kalpet.server.dto.CompteCreditDTO;
import sn.gandal.mobile.kalpet.server.dto.HistoriqueDTO;
import sn.gandal.mobile.kalpet.utils.Java2JsonParser;
import sn.gandal.mobile.kalpet.utils.KalpetConstants;

public class ServerMockHelper extends IDataRemoteAcces{
	
private static ServerMockHelper instance= new ServerMockHelper();
	
public static String[] montantListe = { "12 000 FCFA", "32 840 FCFA",
	"600 FCFA", "2 000 FCFA", "25 000 FCFA" };

public static String[] dateListe = { "20-12-2012", "20-02-2013",
	"20-05-2013", "20-08-2013", "20-10-2013" };

public static String[] localisation = { "Mango Plateau Dakar", "Zara Boulogne Billancourt ",
	"Metro Velizy Villacoublay ", "Sephora Pikine Ouest Face Poste", "ELF Rufisque Ouest Zone B" };

public static String[] beneficiaire = { "Paiement en ligne sur gandal ",
	"Paiement sur partenaire kmEntreprise", "Paiement sur gandal entreprise ",
	"Paiement sur www.senfastfood.com", "Paiement par virement pour Nafy" };
	public static  ServerMockHelper getInstance() {
		 if (null==instance) {
	            instance = new ServerMockHelper();
	        }
		 return instance;
	 }
	
	 
	private ServerMockHelper(){
		
	}
	@Override
	public String get(String addr, String path, Map params) throws ClientProtocolException, IOException {
		String url= addr+path;
		 HttpClient httpclient = new DefaultHttpClient();
    	 HttpGet request=new HttpGet(url);
    	 request.setHeader("content-type", "application/JSON");
    	    HttpResponse response = httpclient.execute(request);
    	    
    	    StatusLine statusLine = response.getStatusLine();
    	    String responseString =null;
    	    if(statusLine.getStatusCode()== HttpStatus.SC_OK){
    	        ByteArrayOutputStream out = new ByteArrayOutputStream();
    	        response.getEntity().writeTo(out);
    	        out.close();
    	        responseString = out.toString();
    	        
    	    } else{
    	        
    	        response.getEntity().getContent().close();
    	        throw new IOException(statusLine.getReasonPhrase());
    	    }
    	
    	return responseString;
	}
	@Override
	public String put(String addr, String path, Map params) throws ClientProtocolException, IOException {
	
    	
    	return null;
	}
	@Override
	public String delete(String addr, String path, Map params) throws ClientProtocolException, IOException {
	
    	
    	return null;
	}


	@Override
	public String getUserData(String numero, String code) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getAccountById(String id) {
		// TODO Auto-generated method stub
		String json=null;
		Account account=new Account();		
		account.setId(26);
		account.setIdExterne(id);
		account.setNom("sow");
		account.setPrenom("Diom");
		account.setAdresse("12 rez cloz");
		account.setTel("0102030405");
		account.setSexe("M");
		
		account.setEnabled(true);
		account.setType("GP");
		account.setDateCreated(new Date(Calendar.getInstance().getTimeInMillis()));
		account.setLastUpdated(new Date(Calendar.getInstance().getTimeInMillis()));
		
		try {
			json =Java2JsonParser.parseObject2Json(account);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json);
		return json;
	}


	@Override
	public String authServer(String codeSecret, String numeroCli) {
		CompteCreditDTO ccredit=new CompteCreditDTO();
		ccredit.setNumero("30866200");
		ccredit.setSolde("2000");
		ccredit.setEnTraitement("1200");
		
		CompteCouponDTO ccoupon = new CompteCouponDTO();
		ccoupon.setEnabled(true);
		ccoupon.setNumeroCompte("CP654321M");
		ccoupon.setDateCreated(new Date(Calendar.getInstance().getTimeInMillis()).toGMTString());
		
		AuthDTO auth= new AuthDTO();
		auth.setNom("SowMock");
		auth.setCodeRetour("200");
		auth.setPrenom("Diom Yero");
		auth.setCni("123456789");
		auth.setAdresse("18 rue beteille 31500 Toulouse");
		auth.setTel("0698804483");
		auth.setSexe("M");
		auth.setCompteActif(true);
		auth.setCompteCredit(ccredit);
		auth.setCompteCoupon(ccoupon);
		
		
		String json=null;
		try {
			 json=Java2JsonParser.parseObject2Json(auth);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json);
		return json;
	}


	@Override
	public String saveAccount(Account account) {
		// generate un fictive id account from server
		String idExterne="123456";
		//creer un compte de credit pour le nouvel utilisateur
		CompteCreditDTO ccredit=new CompteCreditDTO();
		ccredit.setNumero("30866200");
		ccredit.setSolde("0");
		ccredit.setEnTraitement("0");
		//creer un compte de coupon pour le nouvel utilisateur
		CompteCouponDTO ccoupon = new CompteCouponDTO();
		ccoupon.setEnabled(true);
		ccoupon.setNumeroCompte("CP654321M");
		ccoupon.setDateCreated(new Date(Calendar.getInstance().getTimeInMillis()).toGMTString());
		//creer un credentials pour le nouvel utilisateur
		AuthDTO auth= new AuthDTO();
		auth.setNom(account.nom);
		auth.setCodeRetour("200");
		auth.setPrenom(account.prenom);
		auth.setCni("CNI-123456");
		auth.setAdresse(account.adresse);
		auth.setTel(account.tel);
		auth.setSexe(account.sexe);
		auth.setCompteActif(true);
		auth.setCompteCredit(ccredit);
		auth.setCompteCoupon(ccoupon);
		
		auth.setIdExterne(idExterne);
		String json=null;
		try {
			 json=Java2JsonParser.parseObject2Json(auth);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}


	@Override
	public String saveCompteCredit() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String saveCompteCarte() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getFriendsList(String numeroClient, String userPhoneToken,
			Date dateRefresh) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAllHistorics(String numeroClient, String userPhoneToken,
			Date dateRefresh) {
		HistoriqueDTO historique = null;
		
		List<HistoriqueDTO> listHistoriques=new ArrayList<HistoriqueDTO>();
		
		String json=null;
		KalpetConstants.TYPE_COMPTE typeCompte = KalpetConstants.TYPE_COMPTE.CODE;
		for (int i=0; i <5; i++){
			if((i % 2)==0)
				typeCompte=KalpetConstants.TYPE_COMPTE.COUPON;
			if((i % 4)==0)
				typeCompte=KalpetConstants.TYPE_COMPTE.CREDIT;
			
			historique = new HistoriqueDTO();
			historique.setBeneficiaire(beneficiaire[i]);
			historique.setMontant(montantListe[i]);
			historique.setDetails("Details Mock du client "+numeroClient+" numero "+i);
			historique.setLibelle("Libelle Mock du client "+numeroClient+" numero "+i);
			historique.setTypeCompte(typeCompte);
			historique.setLocalisation(localisation[i]);
			historique.setDateTransaction(dateListe[i]);
			
			listHistoriques.add(historique);
			
		}
		try {
			 json=Java2JsonParser.parseObject2Json(listHistoriques);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json);
		return json;
	}

}
